package com.yiqingjing.activity.fragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;
import com.baoyz.swipemenulistview.SwipeMenuListView.OnMenuItemClickListener;
import com.baoyz.swipemenulistview.SwipeMenuListView.OnSwipeListener;
import com.yiqingjing.activity.AboutUsActivity;
import com.yiqingjing.activity.DetailSettingActivity;
import com.yiqingjing.activity.R;
import com.yiqingjing.adapter.ModellistAdapter;

import android.R.integer;
import android.app.Fragment;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

public class ModelListFragment extends Fragment{
	private SwipeMenuListView listView;
	private List<Map<String, Object>>list;
	private ModellistAdapter modellistAdapter;
	private Map<String, Object> map;
	private int[]imgs={R.drawable.icon_normalm,R.drawable.icon_location,R.drawable.icon_outdoor,R.drawable.icon_sleep,R.drawable.icon_timer};

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.modellist, null);
		listView = (SwipeMenuListView)view.findViewById(R.id.model_list);
		list=getData();
		modellistAdapter=new ModellistAdapter(getActivity(), list);
		listView.setAdapter(modellistAdapter);
		SwipeMenuCreator creator = new SwipeMenuCreator() {

			@Override
			public void create(SwipeMenu menu) {
				// create "open" item
				SwipeMenuItem openItem = new SwipeMenuItem(
						getActivity());
				// set item background
				openItem.setBackground(new ColorDrawable(Color.rgb(0xC9, 0xC9,
						0xCE)));
				// set item width
				openItem.setWidth(dp2px(90));
				// set item title
				openItem.setTitle("Open");
				// set item title fontsize
				openItem.setTitleSize(18);
				// set item title font color
				openItem.setTitleColor(Color.WHITE);
				// add to menu
				menu.addMenuItem(openItem);

				// create "delete" item
				SwipeMenuItem deleteItem = new SwipeMenuItem(
						getActivity());
				// set item background
				deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9,
						0x3F, 0x25)));
				// set item width
				deleteItem.setWidth(dp2px(90));
				// set a icon
				deleteItem.setIcon(R.drawable.del_icon_normal);
				// add to menu
				menu.addMenuItem(deleteItem);
			}
		};
		// set creator
		listView.setMenuCreator(creator);

		// step 2. listener item click event
		listView.setOnMenuItemClickListener(new OnMenuItemClickListener() {
			@Override
			public void onMenuItemClick(int position, SwipeMenu menu, int index) {
				map=list.get(position);
				String senceName = map.get("title").toString();
				Integer senceImg=Integer.valueOf(map.get("image").toString());
				switch (index) {
				case 0:
					// open
					
					System.out.println("打开");
					new ModelFragment().updateHead(senceName, senceImg);
					Toast.makeText(getActivity(), senceName+"已开启", 2000);
					break;
				case 1:
					System.out.println("删除");
					list.remove(position);
					modellistAdapter.notifyDataSetChanged();
					break;
				}
			}
		});
		
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				Intent intent=new Intent(getActivity(),DetailSettingActivity.class);
				startActivity(intent);
			}
		});
		
		// set SwipeListener
		listView.setOnSwipeListener(new OnSwipeListener() {
			
			@Override
			public void onSwipeStart(int position) {
				// swipe start
			}
			@Override
			public void onSwipeEnd(int position) {
				// swipe end
			}
		});


		return view;
	}
	
	 public List<Map<String, Object>> getData(){  
	        List<Map<String, Object>> list=new ArrayList<Map<String,Object>>();  
	        for (int i = 0; i < 5; i++) {  
	            Map<String, Object> map=new HashMap<String, Object>();  
	            map.put("image", imgs[i]);  
	            map.put("title", "标准模式"+i);  
	            map.put("info", "这是一个详细信息"+i);  
	            list.add(map);  
	        }  
	        return list;  
	    }  
	 

   private int dp2px(int dp) {
			return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
					getResources().getDisplayMetrics());
		}

}
